package com.wnhoo.smartfishtank.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.wnhoo.smartfishtank.R;

import java.util.HashMap;
import java.util.Map;

public class SoundPoolManager {
	public static final int SOUND_COUNT = 11;

	private SoundPool mSoundPool;
	private Map<Integer, Integer> mSoundMap;
	private Context mContext;
	private int mMaxVolume = 0;
	private static SoundPoolManager instance = null;
	AudioManager mAudioManager;

	public static SoundPoolManager getInstance(Context context) {
		if (instance == null) {
			instance = new SoundPoolManager(context);
		}
		return instance;
	}

	@SuppressLint("UseSparseArrays")
	private SoundPoolManager(Context context) {
		mContext = context;
		//构造方法中第一个为要放多少个文件 第二个是声音类型,第三个声音品质
		mSoundPool = new SoundPool(SOUND_COUNT, AudioManager.STREAM_MUSIC, 100);//
		mSoundMap = new HashMap<Integer, Integer>();
		mSoundMap.put(0, mSoundPool.load(mContext, R.raw.alarm, 1));
		mSoundMap.put(1, mSoundPool.load(mContext, R.raw.alarm_bird, 1));
		mSoundMap.put(2, mSoundPool.load(mContext, R.raw.alarm_car, 1));
		mSoundMap.put(3, mSoundPool.load(mContext, R.raw.alarm_cat, 1));
		mSoundMap.put(4, mSoundPool.load(mContext, R.raw.alarm_chatcall, 1));
		mSoundMap.put(5, mSoundPool.load(mContext, R.raw.alarm_dog, 1));
		mSoundMap.put(6, mSoundPool.load(mContext, R.raw.alarm_fire, 1));
		mSoundMap.put(7, mSoundPool.load(mContext, R.raw.alarm_music, 1));
		mSoundMap.put(8, mSoundPool.load(mContext, R.raw.alarm_radar, 1));
		mSoundMap.put(9, mSoundPool.load(mContext, R.raw.alarm_trumpet, 1));
		mSoundMap.put(10, mSoundPool.load(mContext, R.raw.alarm_whistle, 1));

		mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		
		mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	}

	private int mCurrentStreamId = 0;
private float lv;
private float rv;
private int mindex;
	
	public void play(int index, int volumeLevel, boolean repeat) {
		
		int value = Math.round(((float) volumeLevel / 10) * mMaxVolume);
		if (value < 0) {
			value = 0;
		}
		if (value > mMaxVolume) {
			
			value = mMaxVolume;
		}
		if (mCurrentStreamId != 0) {
			stop();
		}

		mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0);
		
		loopCount = repeat ? -1 : 0;
//		Log.i("CALM", "loopCount"+loopCount);

		lv=(float) volumeLevel / 10;
		rv=(float) volumeLevel / 10;
//			mSoundHandler.sendEmptyMessageDelayed(0x1, 100);
			mindex=index;
//			mSoundHandler.postDelayed(soundRunnable, 0);
			mCurrentStreamId = mSoundPool.play(mSoundMap.get(mindex), lv, rv, 0, loopCount, 1f);
	}
//	private Handler mSoundHandler = new Handler() ;
//
//	private Runnable soundRunnable =new Runnable() {
//		
//		@Override
//		public void run() {
//			mSoundHandler.postDelayed(this, 5*1000);
//		}
//		
//	};

	private int loopCount;
		public void stop() {
//		Log.i("CALM", "soundpool-----------stop");
		mSoundPool.stop(mCurrentStreamId);
//		mSoundHandler.removeCallbacks(soundRunnable);
		mCurrentStreamId = 0;
	}

	public void pause() {
//		Log.i("CALM", "soundpool-----------pause");
		mSoundPool.pause(mCurrentStreamId);
	}
}
